package com.brycen.nio.selector;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * selector练习：客户端
 */
public class Client {
    public static void main(String[] args) throws Exception {
        //创建Socket通道
        SocketChannel channel = SocketChannel.open(new InetSocketAddress("127.0.0.1",9999));
        //设置非阻塞通道
        channel.configureBlocking(false);
        //创建缓冲区
        ByteBuffer buff = ByteBuffer.allocate(1024);
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.print("请输入：");
            String s = scanner.nextLine();
            //粘包、半包测试
//            String s = scanner.nextLine()+'\n'+"1234567890abcdefgh"+'\n';
            //往缓冲区写数据
            buff.put(s.getBytes());
            //归位
            buff.flip();
            //往通道写数据
            channel.write(buff);
            //清空缓冲区
            buff.clear();
        }
    }
}
